/**
 * jims
 */
package com.jims.master.modules.finance.web;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.finance.entity.InpSettleDetail;
import com.jims.master.modules.finance.entity.InpSettleMaster;
import com.jims.master.modules.finance.entity.PatPrepayment;
import com.jims.master.modules.finance.service.PatPrepaymentService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;

import com.jims.master.modules.finance.service.InpSettleMasterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 结算主记录Controller
 * @author 赵宁
 * @version 2017-03-29
 */
@Controller
@RequestMapping(value = "${adminPath}/fiance/inpSettleMaster")
public class InpSettleMasterController extends BaseController {

	@Autowired
	private InpSettleMasterService inpSettleMasterService;
	@Autowired
	private PatPrepaymentService patPrepaymentService;
	@Autowired
	private PatVisitService patVisitService;

	@ModelAttribute
	public InpSettleMaster get(@RequestParam(required=false) String id) {
		InpSettleMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inpSettleMasterService.get(id);
		}
		if (entity == null){
			entity = new InpSettleMaster();
		}
		return entity;
	}
	/**
	 * 出院结算页面
	 * @return String
	 * @Author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"outHospSettle", ""})
	public String outHospSettle() {
		return "modules/finance/outHospSettle";
	}

	/**
	 * 根据住院号查询病人所有预交金信息
	 * @param visitNo
	 * @param patPrepayment
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "getPayments")
	@ResponseBody
	public PatVisit getPayments(String visitNo,PatPrepayment patPrepayment) {
		PatVisit patVisit = patVisitService.findPatInfoByNo(visitNo, UserUtils.getOrgId());
		patPrepayment.setPatVisit(patVisit);
		PatPrepayment patPrepayments= patPrepaymentService.findPrepayments(patPrepayment);
		patVisit.setPatPrepayment(patPrepayments);
		return patVisit;
	}
	/**
	 * 确认结算
	 * @param visitNo
	 * @param payMoney
	 * @param payType
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(String visitNo,String payType,String payMoney,String changes) {
	    StringData data=new StringData();
		Map<String,Object> result =inpSettleMasterService.save(visitNo,payType,payMoney,changes);
		String code = (String)result.get("return");
		String rcptNo=(String)result.get("rcptNo");
        if(StringUtils.isNotBlank(code) && code.equals("0")){
            data.setCode("success");
            data.setData(rcptNo);
        }else{
            data.setCode("error");
            data.setData("保存结算主记录失败");
        }
        return data;
	}

	/**
	 * 取消结算页面
	 * @param rcptNo
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(String  rcptNo,Model model) {
		if(rcptNo!=null){
             InpSettleMaster inpSettleMaster  = inpSettleMasterService.getMasterByRcpt(rcptNo,UserUtils.getOrgId());
             model.addAttribute("inpSettleMaster",inpSettleMaster);
		}
		return "modules/finance/outHospSettleBack";
	}
	/**
	 * 查询结算 主记录
	 * @param rcptNo
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"findSettleMaster", ""})
	@ResponseBody
    public InpSettleMaster findSettleMaster(String rcptNo){
        String orgId= UserUtils.getOrgId();
		InpSettleMaster inpSettleMaster=inpSettleMasterService.getSettleType(rcptNo, orgId);
		return inpSettleMaster;
	}

	/**
	 * 查询结算详情
	 * @param rcptNo
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"searchDetails", ""})
	@ResponseBody
	public List<InpSettleDetail> searchDetails(String  rcptNo){
		 String orgId= UserUtils.getOrgId();
		return  inpSettleMasterService.getDetails(rcptNo,orgId);

	}

	/**
	 * 取消结算
	 * @param rcptNo
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"cancelSettle", ""})
	@ResponseBody
	public StringData cancelSettle(String rcptNo){
		StringData data=new StringData();
		String i=inpSettleMasterService.cancelSettle(rcptNo);
		if(StringUtils.isNotBlank(i) && i.equals("0")){
			data.setCode("success");
			data.setData("保存结算主记录成功");
		}else{
			data.setCode("error");
			data.setData("保存结算主记录失败");
		}
		return data;
	}

	/**
	 * 住院结算查询
	 * @param model
	 * @param inpSettleMaster
	 * @param request
	 * @param response
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value ="settleSearch")
	public String settleSearch(Model model,InpSettleMaster inpSettleMaster,HttpServletRequest request,
							   HttpServletResponse response){
        if(inpSettleMaster.getStartTime()==null || "".equals(inpSettleMaster.getStartTime())){
		   inpSettleMaster.setStartTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
	    }
		if(inpSettleMaster.getEndTime()==null || "".equals(inpSettleMaster.getEndTime())){
			inpSettleMaster.setEndTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		}
		Page<InpSettleMaster> page= inpSettleMasterService.findPage(new Page<InpSettleMaster>(request,response),inpSettleMaster);
		model.addAttribute("page",page);
		model.addAttribute("inpSettleMaster",inpSettleMaster);
      return "modules/finance/inpSettleSearch";
	}

	/**
	 * 出院结算打印
	 * @param inpSettleMaster
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value ="settlePrint")
	@ResponseBody
	public StringData settlePrint(InpSettleMaster inpSettleMaster){
		StringData  data = new StringData();
        String path = inpSettleMasterService.settlePrint(inpSettleMaster);
		data.setCode("success");
		data.setData(path);
		return  data;
	}
}